package com.inei.cie.capituloix;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.inei.cie.Carga_Trabajo_Activity;
import com.inei.cie.Menu_Rural;
import com.inei.cie.R;
import com.inei.cie.capituloi.CapituloI_Capitulos_Activity;
import com.inei.cie.dao.CapituloIIIDao;
import com.inei.cie.utilitarios.ListadoPrediosRuralAdapter;
import com.inei.cie.utilitarios.Variables;

public class CapituloIX_Lista_Predios_Activity extends Activity{
	private TextView tviCodigoLocal;
	private Bundle b;
	private Variables var;
	private ArrayList<String> cantidadPredios;
	private CapituloIIIDao capitulo3Dao;
	private ListadoPrediosRuralAdapter adapter;
	private ListView lviListadoPredios;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_ix_lista_predios);
		b = getIntent().getExtras();
		var = new Variables();
		tviCodigoLocal = (TextView)findViewById(R.id.tviCodigoLocal);
		lviListadoPredios = (ListView)findViewById(R.id.lviListaPredios);
		tviCodigoLocal.setText("Local " + b.getString(var.ID_LOCAL));
		capitulo3Dao = new CapituloIIIDao(getApplicationContext());
		cantidadPredios = (ArrayList<String>) capitulo3Dao.obtenerPredios(b.getString(var.ID_LOCAL)); 
		adapter = new ListadoPrediosRuralAdapter(this,
				cantidadPredios.size(), cantidadPredios);
		lviListadoPredios.setAdapter(adapter);
		
		
		lviListadoPredios.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, final int pos,
					long arg3) {
				
				AlertDialog.Builder dialogo1 = new AlertDialog.Builder(CapituloIX_Lista_Predios_Activity.this);
				dialogo1.setTitle("Eliminar Predio");
				dialogo1.setMessage("Desea eliminar el predio seleccionado?");
				dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
				dialogo1.setCancelable(false);
				dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							capitulo3Dao.eliminarPredio(b.getString(var.ID_LOCAL), adapter.listaPredios.get(pos) , adapter, pos);
						}
					});
				dialogo1.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialogo1, int id) {
								return;
							}
						});
				dialogo1.show();
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
		super.onBackPressed();
		Intent irLocales = new Intent(this, Menu_Rural.class);
		irLocales.putExtras(b);
		startActivity(irLocales);
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_lista_linderos, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir ?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialogo1, int id) {
						System.exit(0);
					}
				});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
